//
//  PageMenuItem.swift
//  Pods
//
//  Created by HOA on 2020/12/29.
//

import UIKit

public enum PageMenuContainerStyle: Int {
    case plain                  = 1
    case cornerRadiusButton     = 2
    case iconButton             = 3
    
    func className() -> String {
        switch self {
        case .plain               : return "PageMenuItem"
        case .cornerRadiusButton  : return "PageMenuItemCornerRadiusButton"
        case .iconButton          : return "PageMenuItemIconButton"
        }
    }
}

protocol Design {
    var color: UIColor { get set }
    var selectedColor: UIColor { get set }
    var backgroundColor: UIColor { get set }
    var selectedBackgroundColor: UIColor { get set }
    var image: UIImage! { get set }
    var selectedImage: UIImage! { get set}
    var font: UIFont { get set }
    var selectedFont: UIFont { get set }
}

public class PageMenuItemDesign: Design {
    
    var color: UIColor = .gray
    var selectedColor: UIColor = .black
    var backgroundColor: UIColor = .clear
    var selectedBackgroundColor: UIColor = .clear
    var image: UIImage! = nil
    var selectedImage: UIImage! = nil
    var font: UIFont = UIFont.systemFont(ofSize: 16)
    var selectedFont: UIFont = UIFont.systemFont(ofSize: 18, weight: .medium)
 
    public init(
        color: UIColor,
        selectedColor: UIColor,
        backgroundColor: UIColor = .clear,
        selectedBackgroundColor: UIColor = .clear,
        image: UIImage! = nil,
        selectedImage: UIImage! = nil,
        font: UIFont = UIFont.systemFont(ofSize: 16),
        selectedFont: UIFont = UIFont.systemFont(ofSize: 18, weight: .medium)) {
    
        self.color = color
        self.selectedColor = selectedColor
        self.backgroundColor = backgroundColor
        self.selectedBackgroundColor = selectedBackgroundColor
        self.image = image
        self.selectedImage = selectedImage
        self.font = font
        self.selectedFont = selectedFont
  }

  public init() {
  }
}

protocol MenuItem {
    
    var title: String { get set }
    var titleColor: UIColor { get set }
    var titleFont: UIFont { get set }
    var image: UIImage! { get set }
    var isSelected: Bool { get set }
    
    func render(active: Bool)
}


public class PageMenuItem: UIView, MenuItem {
        
    public internal(set) var  color: UIColor = .clear
    public internal(set) var design: PageMenuItemDesign = PageMenuItemDesign()
    public internal(set) var  style: PageMenuContainerStyle = .plain
    
    lazy var label: UILabel = {
        let label = UILabel()
        label.text = self.title
        label.textAlignment = .center
        label.backgroundColor = .clear
        label.autoresizingMask = [ .flexibleWidth, .flexibleHeight ]
        return label
    }()
    
    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = self.image
        imageView.backgroundColor = .clear
        imageView.autoresizingMask = [ .flexibleWidth, .flexibleHeight ]
        return imageView
    }()
    
    public required init(coder  aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    public required init(frame: CGRect, title: String, design: PageMenuItemDesign) {
        super.init(frame: frame)
        
        self.backgroundColor = .clear
        self.isUserInteractionEnabled = true
        self.autoresizesSubviews = true
        
        self.title = title
        self.titleColor = design.color
        self.titleFont = design.font
        self.image = design.image
        self.design = design
                
        if image != nil {
            
            let imageViewOriginX: CGFloat = 6
            let imageSize = CGSize(width: 13, height: 16)
            let imageViewOriginY = (bounds.size.height - imageSize.height) / 2.0
            
            imageView.frame = CGRect(origin: CGPoint(x: imageViewOriginX, y: imageViewOriginY), size: imageSize)
            addSubview(imageView)
            let labelOriginX = imageView.frame.maxX + 3
            var labelFrame = self.bounds
            labelFrame.origin.x = labelOriginX
            label.frame = labelFrame
            label.textAlignment = .left
            addSubview(label)
        }
        else {
            label.frame = self.bounds
            self.addSubview(label)
        }
    }
    
    
    /*
     * Properties
     */
    public var title: String = "" {
      willSet {
        if title != newValue {
          label.text = newValue
        }
      }
    }

    var titleColor: UIColor = .black {
      willSet {
        label.textColor = newValue
      }
    }
    
    var titleFont: UIFont = UIFont.systemFont(ofSize: 16) {
      willSet {
        label.font = newValue
      }
    }
    
    var image: UIImage! = nil {
        willSet {
            imageView.image = image
        }
    }

    public var isSelected: Bool = false {
      willSet {
        self.render(active: newValue)
      }
    }

    // MARK: - Overriden Functions
    func render(active: Bool) {
        
        if active {
            titleColor = self.design.selectedColor
            titleFont = self.design.selectedFont
            self.label.backgroundColor = self.design.selectedBackgroundColor
        }
        else {
            self.label.backgroundColor = self.design.backgroundColor
            titleColor = self.design.color
            titleFont = self.design.font
        }
        
    }
}

